package com.terana.lbs.marketing.web.controller;

import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.converter.UserReceiveCouponConverter;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.param.CouponParam;
import com.tarena.lbs.pojo.marketing.param.UserCouponsParam;
import com.tarena.lbs.pojo.marketing.query.CouponQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponCodeQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import com.tarena.lbs.pojo.marketing.vo.CouponVO;
import com.tarena.lbs.pojo.marketing.vo.UserCouponsVO;
import com.terana.lbs.marketing.web.event.input.CouponInput;
import com.terana.lbs.marketing.web.event.output.CouponOutput;
import com.terana.lbs.marketing.web.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
public class CouponController {
    @Autowired
    private CouponService couponService;
    //获取couponOutput通道的注入
    @Autowired
    private CouponOutput couponOutput;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> couponPage(@RequestBody CouponQuery query)
            throws BusinessException {
        PageResult<CouponVO> voPage=couponService.couponPage(query);
        return new Result<>(voPage);
    }

    //优惠券表单新增提交
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> couponSave(@RequestBody CouponParam param)
            throws BusinessException, ParseException {
        couponService.couponSave(param);
        return Result.success();
    }

    //小程序查询优惠券详情
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> couponDetail(@PathVariable Integer id)
            throws BusinessException{
        CouponVO vo=couponService.couponDetail(id);
        return new Result<>(vo);
    }

    @PostMapping("/marketing/user/receive/save")
    public Result<Void> userReceiveSave(@RequestBody UserCouponsParam param) throws BusinessException{

        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("101","认证解析失败"));

        //1.思考 初始化redis库存 key怎么设计的 要和业务绑定 couponId
        String couponStockKey ="coupon:stock:"+param.getCouponId();
        //2.decr coupon:stock:10
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        Long decrement = opsForValue.decrement(couponStockKey);
        //3.判断decrement返回结果是否<0 异常结束
        Asserts.isTrue(decrement<0,new BusinessException("111","您领取的优惠券失败"));
        param.setUserId(userPrinciple.getId());
        Message<UserCouponsParam> message = MessageBuilder.withPayload(param).build();
        //当前系统中定义的output通道
        MessageChannel messageChannel = couponOutput.userGetCouponOutput();
        messageChannel.send(message);
        return Result.success();
    }
    @Autowired
    private UserReceiveCouponConverter userReceiveCouponConverter;
    //查询某个用户的优惠券领取记录
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> userReceiveList(@RequestBody UserCouponQuery query)
            throws BusinessException{
        BasePageList<UserReceiveCouponBO> boPages=couponService.userReceivePage(query);
        //2 利用转化器解析返回voPage
        PageResult<UserCouponsVO> userCouponPage = userReceiveCouponConverter.assemblePageVOS(boPages, query);
        return new Result<>(userCouponPage);
    }
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> userCouponDetail(@RequestBody UserCouponCodeQuery query) throws BusinessException{
        UserReceiveCouponBO bo = couponService.userReceiveDetail(query);
        UserCouponsVO vo = userReceiveCouponConverter.bo2vo(bo);
        return new Result<>(vo);

    }
}
